<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Logic\Sms;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Models\Admin;


class AdminController extends Controller
{

    /**
     * @OA\Post(
     *     path="/admin/admin_info",
     *     tags={"管理员登录"},
     *     summary="信息",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function info()
    {
        $rules = DB::table('role')->where('id',$this->admin['role_id'])->value('rules');
        $rules_arr = explode(',',$rules);
        $menu = DB::table('menu')->whereIn('id',$rules_arr)->select('path')->get()->toArray();
        $menu_arr = array_column($menu,'path');


        $this->admin->rules = $menu_arr;
        return s_msg('',$this->admin);
    }

    /**
     * @OA\Post(
     *     path="/admin/admin_index",
     *     tags={"管理员"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function index()
    {

        $admin_list = Db::table('admin as a')
            ->leftJoin('role as r','a.role_id','=','r.id')
            ->where('a.id','<>',2)
            ->select('a.*','r.name')
            ->paginate();
        return s_msg('',$admin_list);
    }

    /**
     * @OA\Post(
     *     path="/admin/admin_xq",
     *     tags={"管理员"},
     *     summary="详情",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"admin_id"},
     *                 @OA\Property(property="admin_id", type="number", description="管理员id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function xq(Request $request)
    {
        if($request->has('admin_id')){
            $admin_info = Db::table('admin')
                ->where('id',$request->input('admin_id'))
                ->first();
            if($admin_info){
                return s_msg('',$admin_info);
            }else{
                return e_msg(300020,'管理员信息不存在');
            }

        }else{
            return e_msg(300019,'未传入id');
        }

    }
    /**
     * @OA\Post(
     *     path="/admin/admin_add",
     *     tags={"管理员"},
     *     summary="添加",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"username","password"},
     *                 @OA\Property(property="username", type="string", description="用户名"),
     *                 @OA\Property(property="password", type="string", description="密码"),
     *                 @OA\Property(property="role_id", type="integer", description="角色id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function add(Request $request)
    {
        $post_data = $request->all();
        return Admin::add($post_data);
    }
    /**
     * @OA\Post(
     *     path="/admin/admin_edit",
     *     tags={"管理员"},
     *     summary="修改",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"username","password"},
     *                 @OA\Property(property="id", type="integer", description="id"),
     *                 @OA\Property(property="username", type="string", description="用户名"),
     *                 @OA\Property(property="role_id", type="integer", description="角色id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function edit(Request $request)
    {
        $post_data = $request->all();
        return Admin::edit($post_data);
    }

    /**
     * @OA\Post(
     *     path="/admin/admin_change",
     *     tags={"管理员登录"},
     *     summary="修改自己密码",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"password"},
     *                 @OA\Property(property="password", type="string", description="新密码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function change(Request $request)
    {
        if($request->has('password')){

            $id = $this->admin['id'];


            $password = $request->input('password');

            return Admin::reset($id,$password);

        }else{

            return e_msg(300019,'未传入用户新密码 password');

        }

    }
    /**
     * @OA\Post(
     *     path="/admin/admin_reset",
     *     tags={"管理员"},
     *     summary="重置密码",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"uid"},
     *                 @OA\Property(property="id", type="number", description="管理员id"),
     *                 @OA\Property(property="password", type="number", description="新密码"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function reset(Request $request)
    {
        if($request->has('id')){
            $id = $request->input('id');

            if($id == 1){
                return e_msg(123456,'超管密码不能重置');
            }
            if($id == 2){
                return e_msg(123456,'非法操作');
            }

            if($request->has('password') && $request->input('password') ){

                $password = $request->input('password');
            }else{
                return e_msg(123456,'密码不能为空');
            }
            return Admin::reset($id,$password);

        }else{
            return e_msg(300019,'未传入用户id id');
        }

    }

    //curl.cainfo =C:\phpstudy_pro\Extensions\php\php7.3.4nts\cacert.pem


    /**
     * @OA\Post(
     *     path="/admin/admin_delete",
     *     tags={"管理员"},
     *     summary="删除",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"admin_id"},
     *                 @OA\Property(property="admin_id", type="number", description="管理员id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function delete(Request $request)
    {
        if($request->has('admin_id')){
            if($request->input('admin_id') == 1) {
                return e_msg(300021,'第一个管理员不能删除');
            }
            if($request->input('admin_id') == 2) {
                return e_msg(300021,'非法操作');
            }
            $admin_info = Db::table('admin')
                ->where('id',$request->input('admin_id'))
                ->first();
            if($admin_info){
                $rel = DB::table('admin')->where('id',$request->input('admin_id'))->delete();
                return s_msg('');
            }else{
                return e_msg(300020,'管理员信息不存在');
            }

        }else{
            return e_msg(300019,'未传入id');
        }

    }
}
